import 'package:OASys/api/UserApi.dart';
import 'package:OASys/components/FormComponents.dart';
import 'package:OASys/components/scaffoldItem.dart';
import 'package:OASys/entity/UserAdd.dart';
import 'package:OASys/entity/UserVo.dart';
import 'package:OASys/utils/validateUtils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class UserInfoEditPage extends StatelessWidget {
  final Map arguments;

  const UserInfoEditPage({Key key, this.arguments}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ScaffoldItem(
        '编辑用户信息', MyUserInfoEditPage(UserVo.fromJson(arguments)));
  }
}

class MyUserInfoEditPage extends StatefulWidget {
  final UserVo userVo;

  MyUserInfoEditPage(this.userVo, {Key key}) : super(key: key);

  @override
  _MyUserInfoEditPageState createState() => _MyUserInfoEditPageState();
}

class _MyUserInfoEditPageState extends State<MyUserInfoEditPage> {
  GlobalKey<FormState> formKey = GlobalKey<FormState>();

  UserAdd info = new UserAdd();

  @override
  void initState() {
    super.initState();
    info = UserAdd.fromJson(widget.userVo.toJson());
  }

  void save() {
    var form = formKey.currentState;
    if (form.validate()) {
      form.save();
      print(info.toJson().toString());
    }
    UserApi().save(
      info,
      success: (res) {
        showMessage(context, "修改成功");
        Navigator.of(context).pop(true);
      },
      error: (err) {
        print(err.message);
      },
    );
  }

  void reset() {
    var form = formKey.currentState;
    form.reset();
    setState(() {
      info.status = int.parse(widget.userVo.status.toString());
    });
  }

  show3() {
    showModalBottomSheet(
        context: context,
        builder: (context) {
          return Container(
            height: 300,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                FlatButton(
                  child: Text('A'),
                  onPressed: () {
                    print('object');
                    setState(() {
                      info.status = 2;
                    });
                    Navigator.of(context).pop(info.status);
                  },
                ),
                ListTile(
                  title: Text('B'),
                  subtitle: Text('bbbb'),
                  onTap: () {
                    print('object');
                  },
                ),
                ListTile(
                  title: Text('C'),
                  subtitle: Text('cccc'),
                  onTap: () {
                    print('object');
                  },
                ),
                ListTile(
                  title: Text('C'),
                  subtitle: Text('cccc'),
                  onTap: () {
                    print('object');
                  },
                ),
                ListTile(
                  title: Text('C'),
                  subtitle: Text('cccc'),
                  onTap: () {
                    print('object');
                  },
                ),
                ListTile(
                  title: Text('C'),
                  subtitle: Text('cccc'),
                  onTap: () {
                    print('object');
                  },
                )
              ],
            ),
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 900,
      width: 400,
      margin: EdgeInsets.all(45),
      child: Card(
        child: Container(
          margin: EdgeInsets.all(20),
          child: Form(
            key: formKey,
            child: Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    PrefixContanier('用户名：'),
                    FormInput(
                      initValue: info.username,
                      hintText: "请输入用户名",
                      validator: (content) {
                        if (content.length > 12) {
                          return "用户名过长";
                        } else if (content.length <= 0) {
                          return "用户名过短";
                        }
                      },
                      onSave: (value) {
                        info.username = value;
                      },
                    ),
                  ],
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    PrefixContanier('昵称：'),
                    FormInput(
                      initValue: info.nickname,
                      hintText: "请输入昵称",
                      validator: (content) {
                        if (content.length > 8) {
                          return "昵称过长";
                        } else if (content.length <= 0) {
                          return "昵称过短";
                        }
                      },
                      onSave: (value) {
                        info.nickname = value;
                      },
                    ),
                  ],
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    PrefixContanier('手机号码：'),
                    FormInput(
                      initValue: info.phone,
                      hintText: "请输入手机号码",
                      validator: (content) {
                        if (ValidateUtils.isNotPhone(content)) {
                          return "手机号码错误";
                        }
                      },
                      onSave: (value) {
                        info.phone = value;
                      },
                    ),
                  ],
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    PrefixContanier('邮箱：'),
                    FormInput(
                      initValue: info.email,
                      hintText: "请输入邮箱",
                      validator: (content) {
                        if (ValidateUtils.isNotEmail(content)) {
                          return "请输入正确的邮箱地址";
                        }
                      },
                      onSave: (value) {
                        info.email = value;
                      },
                    ),
                  ],
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    Flexible(
                      flex: 2,
                      child: PrefixContanier('性别：'),
                    ),
                    Flexible(
                      flex: 1,
                      child: RadioListTile(
                        title: Text('男'),
                        value: 1,
                        groupValue: info.sex,
                        onChanged: (value) {
                          setState(() {
                            info.sex = value;
                          });
                        },
                      ),
                    ),
                    Flexible(
                      flex: 1,
                      child: RadioListTile(
                        title: Text('女'),
                        value: 2,
                        groupValue: info.sex,
                        onChanged: (value) {
                          setState(() {
                            info.sex = value;
                          });
                        },
                      ),
                    ),
                  ],
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    PrefixContanier('状态：'),
                    Text(info.status == 0 ? '启用' : '禁用'),
                    Switch(
                      value: info.status == 0,
                      activeColor: Colors.green,
                      inactiveTrackColor: Colors.red,
                      onChanged: (value) {
                        setState(() {
                          info.status = value ? 0 : 1;
                        });
                      },
                    ),
                  ],
                ),
                SizedBox(
                  height: 30,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    RaisedButton(
                      color: Color.fromRGBO(33, 150, 243, 1),
                      onPressed: save,
                      child: Text(
                        "保 存",
                        style: TextStyle(color: Colors.white),
                      ),
                    ),
                    RaisedButton(
                      color: Color.fromRGBO(103, 194, 58, 1),
                      onPressed: reset,
                      child: Text(
                        "重 置",
                        style: TextStyle(color: Colors.white),
                      ),
                    )
                  ],
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}
